package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.work.masterwork.MasterWorkBService;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.dto.Retain24hourPartDTO;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.work.masterwork.MasterPartRetainDTO;
import com.zmn.oms.model.dto.work.masterwork.UsePartDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.masterwork.ZsMasterWorkBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author zhubin
 * @version 1.0.0
 * @ClassName OrderUsePartListener.java
 * @Description 订单使用配件相关  由scm2推送
 * @createTime 2022年10月08日 09:54:00
 */
@Slf4j
@Component
@AliyunMQListener(topic = "${spring.mq.topic.scm2}")
public class OrderUsePartListener {

    @Resource
    private ZsMasterWorkBService zsMasterWorkBService;

    @Resource
    private OrderWorkService orderWorkService;

    @Resource
    private MasterWorkBService masterWorkBService;

    /**
     * 工单修改使用配件消息监听
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.ORDER_USE_PART_TAG)
    public void modifyUsePart(String message){
        log.info("#MQ#modifyUsePart# {}", message);
        if (StringUtil.isBlank(message)) {
            return;
        }
        try {
            UsePartDTO usePartDTO = JSONObject.parseObject(message, UsePartDTO.class);
            if (Objects.isNull(usePartDTO)){
                return;
            }
            zsMasterWorkBService.updateUsePart(usePartDTO);
        } catch (Exception e) {
            log.error("#MQ#modifyUsePart  exception {},{}", e,e.getMessage());
        }
    }


    /**
     * 保留配件申请24小时
     * @param
     * @return
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.ORDER_RETAIN_24HOURS_PARTS)
    public void retain24hoursParts(String message) {
        log.info("MQ#retain24hoursParts保留配件申请24小时=>入参:{}", JSON.toJSONString(message));
        try {
            MasterPartRetainDTO masterPartRetainDTO = JSONObject.parseObject(message, MasterPartRetainDTO.class);
            if (Objects.equals(masterPartRetainDTO.getRetainPart(), GlobalConsts.YES)) {
                OrderWork orderWork = orderWorkService.findOrderWorkByKey(masterPartRetainDTO.getOrderId(), masterPartRetainDTO.getWorkId());
                if (Objects.isNull(orderWork)) {
                    log.info("MQ#retain24hoursParts error=>订单不存在=>[{}]",message);
                    return;
                }
                if (orderWork.getStatus() >= OrderStatusConsts.WORK_STATUS_CHECKOUT) {
                    log.info("MQ#retain24hoursParts error=>已收单不能操作保留配件=>[{}]",JSONObject.toJSONString(orderWork));
                    return;
                }
                // 已完成后操作保留配件--新增提醒任务
                if (Objects.equals(orderWork.getPartRetain(),GlobalConsts.NO)
                        && Objects.equals(orderWork.getStatus(), OrderStatusConsts.WORK_STATUS_COMPLETE)) {
                    try {
                        zsMasterWorkBService.addRetainPartJob(orderWork, DateUtil.getNow());
                    } catch (Exception e) {
                        log.error(e.getMessage(),e);
                    }
                }
            }
            masterWorkBService.updatePartRetain(masterPartRetainDTO);
        }catch (Exception e){
            log.error("#MQ#retain24hoursParts  exception {},{}", e,e.getMessage());
        }

    }
}
